package com.foundryx.shared.exception.i18n;

import com.foundryx.shared.exception.message.MessageResolver;
import com.foundryx.shared.exception.message.PropertiesBasedMessageResolver;

/**
 * @className: MessageLookup
 * @author: ocxz1
 * @date: 2025/6/20 下午2:01
 * @Version: 1.0
 * @description: fx-share-resources
 */
public class MessageLookup {

    private static MessageResolver resolver = new PropertiesBasedMessageResolver("messages");
    private static LocaleHold localeHold = new LocaleContext();

    /**
     * 设置消息解析器
     * @param resolver 消息解析器
     */
    public static void setMessageResolver(MessageResolver resolver) {
        MessageLookup.resolver = resolver;
    }

    /**
     * 设置语言环境（如果是异步调用，则需要设置）
     * @param localeHold 语言环境
     */
    public static void setLocaleHold(LocaleHold localeHold) {
        MessageLookup.localeHold = localeHold;
    }

    /**
     * 获取消息
     * @param code 消息编码
     * @param args 参数
     * @return 消息
     */
    public static String getMessage(String code, Object... args) {
        return resolver.resolveMessage(code, args, localeHold.getLocale());
    }
}
